package com.zsh.web;

import com.google.gson.Gson;
import com.zsh.pojo.Book;
import com.zsh.pojo.Cart;
import com.zsh.pojo.CartItem;
import com.zsh.service.BookService;
import com.zsh.service.impl.BookServiceImpl;
import com.zsh.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CartServlet extends BaseServlet {

        private BookService bookService = new BookServiceImpl();

        protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            int id = WebUtils.parseInt(request.getParameter("id"),0);
            int count = WebUtils.parseInt(request.getParameter("count"),1);

            Cart cart = (Cart) request.getSession().getAttribute("cart");

            if (cart != null){
                cart.updateItem(id,count);
                response.sendRedirect(request.getHeader("Referer"));
            }

        }



        protected void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Cart cart = (Cart) request.getSession().getAttribute("cart");
            if (cart != null) {
                cart.clear();
                response.sendRedirect(request.getHeader("Referer"));
            }
        }

        protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            int id = WebUtils.parseInt(request.getParameter("id"),0);

            Cart cart = (Cart) request.getSession().getAttribute("cart");

            if (cart != null){
                cart.deleteItem(id);
                response.sendRedirect(request.getHeader("Referer"));
            }


        }


        protected void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("addCart");
        System.out.println(request.getParameter("bookId"));

        int id = WebUtils.parseInt(request.getParameter("bookId"),0);

        Book book = bookService.queryBookById(id);

        CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());

//        Cart cart = new Cart(); 应该放在session中，不然每次都是新的购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        if (cart == null){
            cart = new Cart();
            request.getSession().setAttribute("cart",cart);
        }
        cart.addItem(cartItem);

        System.out.println(cart);

        //最后添加的商品名称
        request.getSession().setAttribute("lastName",cartItem.getName());

        response.sendRedirect(request.getHeader("Referer"));
        }

        protected void ajaxAddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            int id = WebUtils.parseInt(request.getParameter("bookId"),0);

            Book book = bookService.queryBookById(id);

            CartItem cartItem = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());

//        Cart cart = new Cart(); 应该放在session中，不然每次都是新的购物车
            Cart cart = (Cart) request.getSession().getAttribute("cart");

            if (cart == null){
                cart = new Cart();
                request.getSession().setAttribute("cart",cart);
            }
            cart.addItem(cartItem);

//            System.out.println(cart);

            //最后添加的商品名称
            request.getSession().setAttribute("lastName",cartItem.getName());

            Map<String,Object> resultMap = new HashMap<String,Object>();

            resultMap.put("totalCount",cart.getTotalCount());
            resultMap.put("lastName",cartItem.getName());

            Gson gson = new Gson();
            String json = gson.toJson(resultMap);

            response.getWriter().write(json);

        }
}
